package com.ptadmin.report.executor;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.mws.model.IdList;
import com.ptadmin.report.mws.RequestReportWrapper;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class RequestReportExecutor {
	
	private static final Logger logger = LoggerFactory.getLogger(RequestReportExecutor.class);
	
	private List<RequestReportParameter> parameters;
	private List<RequestReportResult> results;
	
	private LocalDate startDate;
	private LocalDate endDate;
	
	private RequestReportExecutor(Builder builder) {
		this.parameters = new ArrayList<RequestReportParameter>();
		
		if (builder.merchantIdList.size() != builder.sellerDevAuthTokenList.size()) {
			throw new RuntimeException("builder error, merchantId size is not equal to sellerDevAuthToken size...");
		}
		
		this.startDate = builder.startDate;
		this.endDate = builder.endDate;

		for (int i=0; i < builder.merchantIdList.size(); i++) {
			
			for (String reportType : builder.reportTypeList) {
				RequestReportParameter p = new RequestReportParameter();
				p.setMerchantId(builder.merchantIdList.get(i));
				p.setSellerDevAuthToken(builder.sellerDevAuthTokenList.get(i));
				p.setReportType(reportType);
				p.setMarketplaceIdList(new IdList(builder.marketplaceIdList));
				this.parameters.add(p);
			}
		}
	}
	
	public static class Builder {
		private List<String> merchantIdList = new ArrayList<String>();
		private List<String> sellerDevAuthTokenList = new ArrayList<String>();
		
		private List<String> reportTypeList = new ArrayList<String>();
		private List<String> marketplaceIdList = new ArrayList<String>();
		private LocalDate startDate = LocalDate.now();
		private LocalDate endDate = LocalDate.now().plusDays(1);
		
		public Builder with(String merchantId, String sellerDevAuthToken) {
			this.merchantIdList.add(merchantId);
			this.sellerDevAuthTokenList.add(sellerDevAuthToken);
			return this;
		}
		
		public Builder withReportType(String reportType) {
			this.reportTypeList.add(reportType);
			return this;
		}
		
		public Builder withMarketplaceId(String marketplaceId) {
			this.marketplaceIdList.add(marketplaceId);
			return this;
		}
		
		public Builder withStartDate(LocalDate startDate, long period) {
			this.startDate = startDate;
			this.endDate.plusDays(period);
			return this;
		}
		
		public RequestReportExecutor build() {
			RequestReportExecutor executor = new RequestReportExecutor(this);
			return executor;
		}
	}
	
	@Getter
	@Setter
	@NoArgsConstructor
	@AllArgsConstructor
	private class RequestReportParameter {
		private String merchantId;
		private String sellerDevAuthToken;
		private String reportType;
		private IdList marketplaceIdList;
	}

	@Getter
	@Setter
	@NoArgsConstructor
	@AllArgsConstructor
	private class RequestReportResult {
		private String requestReportId;
		private String reportType;
		private String reportName;
		private String reportExt;
	}
	
	public List<String> execute() {
		// RequestReport
		List<String> requestReportIdList = new ArrayList<String>();
		for (RequestReportParameter p : parameters) {
			logger.info(String.format(">>>>>>>>> request report RequestReportParameter: %s", p.toString()));
			RequestReportWrapper requestReport = new RequestReportWrapper();
			requestReport.buildRequest(p.getMerchantId(), p.getSellerDevAuthToken(), p.getMarketplaceIdList(), p.getReportType(), this.startDate, this.endDate);
			requestReport.invokeRequest();
			requestReport.printResponse();
			
			requestReportIdList.add(requestReport.getReportRequestId());
		}
		
		return requestReportIdList;
	}
}
